perm filename ABBISC[1,RWF] blob
sn#734378 filedate 1983-12-08 generic text, type C, neo UTF8
COMMENT ⊗ VALID 00003 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002 Programs
C00005 00003 Standard Algorithms in Pascal
C00009 ENDMK
C⊗;
Programs
A ←program← in Pascal is a complete algorithm, describing a computation in
sufficient detail that it can be carried out by a computer. A Pascal
program contains information about the resources the computer will need to
perform the computation, and detailed instructions about the sequence of
computational steps. The simplest type of Pascal program has the form
PROGRAM name (OUPUT);
BEGIN
command;
command;
...
...
command
END.
where the programmer fills in the name of his program where `name' appears
in the form, and fills in commands to carry out the desired computation
where `command' appears in the form.
Program names can be made up at will from the English alphabet. There are
many kinds of commands, but every program will contain some commands to
print its results so that we can see them. The form of such a command can be
WRITE (expression, expression, ..., expression)
where the expressions can be numbers, or forumlas built up from numbers
using symbols that express the operations of arithmetic and elementary
functions.
1. (Material from my published Pascal Notes goes here)
We need a compact, unambiguous way to express the forms of programs,
commands, etc., that are acceptable in Pascal. We will use ←transition
diagrams← for these forms. The transition diagram for programs is:
program: PROGRAM name (OUTPUT);
BEGIN command ;
END.
Any path through the diagram is a legitimate form for a program. Lower
case names, in rectangular boxes, like "command" stand for sections which
the programmer must fill in. In the diagram above, every time we come to
"command", we have to fill in a legitimate command according to another
transition diagram:
command: WRITE ( expression ,
WRITELN )
Standard Algorithms in Pascal
Electronic computers have built-in algorithms for elementary arithmetic.
Usually the built-in algorithms include addition, subtraction,
multiplication, division with optional remainder, and testing whether one
number is greater than, equal to, or less than, another. Pascal uses these
algorithms. A Pascal programmer need not concern himself with how division
is done; he writes 22/7 in his program, and the result 3.142857 is
automatically supplied. Other functions, including the log, trig, and
exponential functions, are not built into the computer, but can be comuted
by small programs that the Pascal translator automatically includes in any
program that needs them. If a Pascal programmer writes the expression
LOG(2) + TAN(3.1415926/4), the translator provides for finding the
logarithm, adding, etc., by a fast and accurate algorithm. The forms of
expression in Pascal that use the standard numerical algorithms are these,
where A and B are numbers, or expressions with numerical values.
A + B, A - B, A/B, SIN(A), COS(A), ... have their customary meaning.
A * B is the product of A and B. Remember not to use A x B; Pascal won't
understand.
SQRT(A) is the positive square root of A (A>=0)
SQR(A) is A↑2 (Don't confuse SQRT with SQR)
LN(A) is the natural (base = 2.71828...) logarithm.
LOG(A) is the common (base 10) logarithm.
...
...
When several standard algorithms are used in the same expression, as in
2 + 3/4 * 5 - 6, a set of conventions determine what is done first.
Generally, where possible, multiplications and divisions are done first,
from left to right, giving 3/4 = 0.75 and 0.75 * 5 = 3.75; next additions
and subtractions are done from left to right, giving
2 + 3.75 = 5.75 and 5.75 - 6 = -0.25, the value of the expression.
Parentheses may be used where the convention is not the same as the
programmer's intention; 2 + 3/(4 * 5) - 6 gives
4 * 5 = 20, 3/20 = 0.15, 2 + 0.15 = 2.15, 2.15 - 6 = -3.85.